'use strict';

app.controller('memberCtrl', function ($scope, $http) {
    // console.log("ccccccccccc");
    $scope.member_form = [
    	{
    		"id":"1564654",
            "name":"张三",
    		"phone":"13012345678",
    		"time":"2017-05-01",
    		"stored_money":323,
    		"grand_total":1055
    	},
    	{
    		"id":"4165464",
            "name":"王大陆",
    		"phone":"13012345678",
    		"time":"2017-04-23",
    		"stored_money":323,
    		"grand_total":1055
    	},
    	{
    		"id":"514654131",
            "name":"李贵",
    		"phone":"13012345678",
    		"time":"2017-03-11",
    		"stored_money":323,
    		"grand_total":1055
    	},
    	{
    		"id":"146541",
            "name":"张宇",
    		"phone":"13012345678",
    		"time":"2017-03-27",
    		"stored_money":323,
    		"grand_total":1055
    	},
    	{
    		"id":"56525",
            "name":"陈文",
    		"phone":"13012345678",
    		"time":"2017-02-12",
    		"stored_money":323,
    		"grand_total":1055
    	},
    	{
    		"id":"325567464",
            "name":"赵兴",
    		"phone":"13012345678",
    		"time":"2017-05-21",
    		"stored_money":323,
    		"grand_total":1055
    	},
    	{
    		"id":"574835132132",
            "name":"杨丽",
    		"phone":"13012345678",
    		"time":"2017-05-05",
    		"stored_money":323,
    		"grand_total":1055
    	},
    	{
    		"id":"548742103",
            "name":"孙齐全",
    		"phone":"13012345678",
    		"time":"2017-03-10",
    		"stored_money":323,
    		"grand_total":1055
    	},
    	{
    		"id":"468745415",
            "name":"王黎明",
    		"phone":"13012345678",
    		"time":"2017-05-04",
    		"stored_money":323,
    		"grand_total":1055
    	},
    	{
    		"id":"8674868464",
            "name":"赵婷",
    		"phone":"13012345678",
    		"time":"2017-02-01",
    		"stored_money":323,
    		"grand_total":1055
    	},
        {
            "id":"53416542313",
            "name":"赵兴",
            "phone":"13012345678",
            "time":"2017-05-21",
            "stored_money":323,
            "grand_total":1055
        },
        {
            "id":"356563123652",
            "name":"杨丽",
            "phone":"13012345678",
            "time":"2017-05-05",
            "stored_money":323,
            "grand_total":1055
        },
        {
            "id":"69513113",
            "name":"孙齐全",
            "phone":"13012345678",
            "time":"2017-03-10",
            "stored_money":323,
            "grand_total":1055
        },
        {
            "id":"4165414313",
            "name":"王黎明",
            "phone":"13012345678",
            "time":"2017-05-04",
            "stored_money":323,
            "grand_total":1055
        },
        {
            "id":"15464613123",
            "name":"赵婷",
            "phone":"13012345678",
            "time":"2017-02-01",
            "stored_money":323,
            "grand_total":1055
        }
    ];
    
    $(document).ready(function(){ 
        $('.tableFilter').dataTable({ 
            "oLanguage": { 
                "sLengthMenu": "每页显示 _MENU_ 条记录", 
                "sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据", 
                "sInfoEmpty": "没有数据", 
                "sSearch" : "搜索:",//搜索框前的文本设置
                "sInfoFiltered": "(从 _MAX_ 条数据中检索)", 
                "oPaginate": { 
                    "sFirst": "首页", 
                    "sPrevious": "前一页", 
                    "sNext": "后一页", 
                    "sLast": "尾页"
                }, 
                "sZeroRecords": "没有检索到数据", 
                // "sProcessing": "<img src='./loading.gif' />"

            },
            "aaSorting": [//按照入会时间排序：desc或asc
                [ 4, "desc" ]
            ]
            
        }); 

    });

    // 生成时间id号
    $scope.idTostring = function(){
        var now = new Date();
        var year = now.getFullYear();
        var month =(now.getMonth() + 1).toString();
        var day = (now.getDate()).toString();
        var hour = (now.getHours()).toString();
        var minute = (now.getMinutes()).toString();
        var second = (now.getSeconds()).toString();
        if (month.length == 1) {
            month = "0" + month;
        }
        if (day.length == 1) {
            day = "0" + day;
        }
        var dateTime = year + month +  day + hour + minute + second;
        return dateTime;
    };

    // 生成日期
    $scope.creatDate = function(){
        var now = new Date();
        var year = now.getFullYear();
        var month =(now.getMonth() + 1).toString();
        var day = (now.getDate()).toString();
        if (month.length == 1) {
            month = "0" + month;
        }
        if (day.length == 1) {
            day = "0" + day;
        }
        var dateTime = year + '-' + month + '-' +  day;
        return dateTime;
    };

    $scope.add_member = {};
    // 添加会员
    $scope.addMember = function(){

        // $scope.add_member.push("aaaaa");
        $scope.add_member.id = $scope.idTostring();
        $scope.add_member.time = $scope.creatDate();
        $scope.add_member.grand_total = 0;
        var a = parseInt($scope.add_member.stored_money);//转成整型
        $scope.add_member.stored_money = a;

        // $scope.add_member.push({
        //     "id":$scope.idTostring(),
        //     "time":$scope.creatDate(),
        //     "grand_total":0
        // });
        console.log($scope.add_member);
        // $http.post('/api/addMember',$scope.add_member).then(function(response){
        //     if(response.status==200){
        //         console.log(response);
        //         $scope.add_member = {};
        //     }
        //     else{
        //         console.log(response.status);
        //     }
        // });

    };

    // 点击编辑会员
    $scope.editMember = function(index){
        // console.log(index);
        $scope.edit_member = $scope.member_form[index];
        // console.log($scope.edit_member);
    };

    // 更新会员
    $scope.updateMember = function(index){
        $scope.edit_member.stored_money = parseInt($scope.edit_member.stored_money) + parseInt($scope.edit_member.recharge);
        console.log($scope.edit_member);
        $http.post('/api/updateMember',$scope.edit_member).then(function(response){
            if(response.status==200){
                console.log(response);
            }
            else{
                console.log(response.status);
            }
        });
        
    };

    // 删除会员
    $scope.removeMember = function(index){
        $scope.remove_member = $scope.member_form[index];
        $http.delete('/api/deleteMember',$scope.remove_member).then(function(response){
            if(response.status==200){
                console.log(response);
                // $("#table_server").DataTable().draw();//点搜索重新绘制table。
            }
            else{
                console.log(response.status);
            }
        });
        
    };




    
});